<template>
  <div class="addArticle-page">
    <GhNavBar class="head" title="添加文章"  rightText="发布" @click-right="clickRight" />
    <div class="content">
        <van-field  v-model="article.title"   placeholder="请输入文字标题"  label="文章标题" required label-width="70px" autocomplete="off" ></van-field>
        <van-field  v-model="article.picture"   placeholder="请输入图片链接"  label="图片链接" required label-width="70px" autocomplete="off" ></van-field>
        <van-field
              v-model="article.content"
              class="con_box"
              label="内容"
              type="textarea"
              label-width="30px"
              autosize
              rows="15" 
              placeholder="请输入文章内容"
              required
          ></van-field>
    </div>
  </div>
</template>

<script setup>
import {ref,reactive,onBeforeMount,computed,onMounted,watch} from "vue"
import { Toast, Dialog } from 'vant';
import 'vant/es/toast/style'
import{useRouter,useRoute}from 'vue-router'
const router =useRouter();  
const route =useRoute()
import{getconsultantInfoByaccount,addArticle}from '@/services/consultant'

//定义文章信息响应式变量
const article=ref({
    title:'',
    content:'',
    picture:'',
    author:''
})


//确定发布文章
const clickRight=async()=>{
    if(article.value.title.trim()===''){
        Toast('请输入标题！')
        return
    }
    if(article.value.picture.trim()===''){
        Toast('请输入图片链接！')
        return
    }
    if(article.value.content.trim()===''){
        Toast('请输入文章内容！')
        return
    }
    await Dialog.confirm({
    title: '温馨提示',
    message: '您确认要发布文章吗？'
  })
   .then(async() => {
    const account=JSON.parse(localStorage.getItem('gh-user')).user.userData.account
    const consultantInfo=await getconsultantInfoByaccount(account)
    article.value.author=consultantInfo.data.name
    const formData=new FormData();
    formData.append('title',article.value.title)
    formData.append('content',article.value.content)
    formData.append('picture',article.value.picture)
    formData.append('author',article.value.author)
    const res=await addArticle(formData).then((response)=>{
      return response
    }).catch((error)=>{
     return error
    })
//    console.log(res)
 if(res.status!==201){
    if(res.response.status==402){
        const toastMessage=res.response.data.error
        Toast.fail(toastMessage)
        isRegistering.value=false;
        return
    }
    if(res.response.status==404){
        const toastMessage=res.response.data.error
        Toast.fail(toastMessage)
        isRegistering.value=false;
        return
    }
    }
    router.push('/articlePublish')
    Toast.success('发布成功！')
  })
  .catch(() => {
    router.push('/articlePublish')
    Toast('取消发布，暂存为草稿')
  });
    // console.log('发布文章')
}



</script>

<style lang="scss" scoped>
.addArticle-page{
    width: 100%;
    .head{
        background-color: #f5f5f5;
    }
    .content{
        margin-top: 44px;
        width: 100%;
         .con_box{
            border: 1px solid #918585;
         }

    }
}
</style>
